import time

from sqlalchemy.exc import SQLAlchemyError

from Hook import hook
import os
from flask import Blueprint, request, make_response
from home import redisDB, db
from model import JobModel, EnterpriseModel, HRModel, EInfoConfirmingModel, EJobConfirmingModel, UserAndJobModel, \
    UserModel, UserResumeModel, HomeMenuModel
from home.config import BASE_DIR
from werkzeug.security import check_password_hash, generate_password_hash

bp = Blueprint("enterprise", __name__, url_prefix="/enterprise")


# 求职者学历分布
@bp.route("/getJobSeekerEdu", methods=["POST"])
def getJobSeekerEdu():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "dataX": [],
        "dataY": [],
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    edus = []
                    # 获取hr所在企业所有的hr
                    hrs = hr.e_hr.hrs
                    for i in hrs:
                        # 获取hr所在企业所有hr所发布的岗位，即该企业所发布的全部岗位
                        for j in i.hr_jobs:
                            # 获取该公司被用户申请的所有岗位
                            userAndJobModel = UserAndJobModel.query.filter_by(jobId=j.jobId).all()
                            if userAndJobModel:
                                for userJob in userAndJobModel:
                                    # 通过岗位和求职者关系表查找到对应的uid，再查找对应的求职者，之后再查找求职者简历
                                    #     user = UserModel.query.filter_by(uId=userJob.uId).first()
                                    userResume = UserResumeModel.query.filter_by(uId=userJob.uId).first()

                                    edus.append(userResume.eduEducation if userResume and userResume.eduEducation else "未填写")
                    from collections import Counter
                    res=Counter(edus)
                    msg["dataX"]=['未填写','初中', '高中','大专', '本科', '研究生']
                    msg["dataY"]=[res["未填写"],res["初中"],res["高中"],res["大专"],res["本科"],res["研究生"]]
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"


                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取求职者工作经验分布
@bp.route("/getJobSeekerJobEx", methods=["POST"])
def getJobSeekerJobEx():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "dataY": [],
        "dataX": []
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    users = []
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                # 获取当前招聘者身份
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    # 获取hr所在企业所有的hr
                    hrs = hr.e_hr.hrs
                    for i in hrs:
                        # 获取hr所在企业所有hr所发布的岗位，即该企业所发布的全部岗位
                        for j in i.hr_jobs:
                            # 获取该公司被用户申请的所有岗位
                            userAndJobModel = UserAndJobModel.query.filter_by(jobId=j.jobId).all()
                            if userAndJobModel:
                                for userJob in userAndJobModel:
                                    # 通过岗位和求职者关系表查找到对应的uid，之后再查找求职者简历
                                    userResume = UserResumeModel.query.filter_by(uId=userJob.uId).first()
                                    # 获取求职者的工作时间
                                    jobTime = int(userResume.jobExTime.split("-")[1].split(".")[0]) - int(
                                        userResume.jobExTime.split("-")[0].split(".")[
                                            0]) if userResume and userResume.jobExTime else 0
                                    users.append(jobTime)

                    from Hook.hook import getJobExDate
                    res = getJobExDate(users)

                    msg["dataX"] = ['1年以下', '1-2年', '2-3年', '3-5年', '5-8年', '8-10年', '10年以上']

                    msg["dataY"] = [res["1年以下"], res["1-2年"], res["2-3年"], res["3-5年"], res["5-8年"], res["8-10年"],
                                    res["10年以上"]]
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 企业用户--查看 具体的 审核的岗位信息 及审核状态
@bp.route("/ePreAuditJobs", methods=["POST"])
def ePreAuditJobs():
    global operationType
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    auditId = request.json["auditId"]
                    job = EJobConfirmingModel.query.filter_by(auditId=auditId).first()
                    if job:
                        # hr = HRModel.query.filter_by(hrId=job.hrId).first()
                        if job.operationType == "add":
                            operationType = "添加岗位"
                        elif job.operationType == "update":
                            operationType = "编辑岗位"
                        msg["datas"].append(
                            {
                                "auditId": job.auditId,
                                "jobId": job.jobId,
                                "jobName": job.jobName,
                                "jobNature": job.jobNature,
                                "jobAddress": job.jobAddress,
                                "jobEdu": job.jobEdu,
                                "jobAge": job.jobAge,
                                "jobEx": job.jobEx,
                                "jobSalary": job.jobSalary,
                                "jobDetail": job.jobDetail,
                                "jobIndustry": job.jobIndustry,

                                "hrId": job.hrId,
                                "hrName": job.hrName,
                                "hrEmail": job.hrEmail,
                                "hrPhone": job.hrPhone,

                                "isAudit": job.isAudit,
                                "auditTime": job.auditTime,
                                "confirmTime": job.confirmTime,
                                "auditState": job.auditState,
                                "operationType": operationType,
                                "eId": hr.e_hr_id,
                                "businessName": hr.e_hr.businessName,
                                "auditResultContent": job.auditResultContent,
                            }
                        )
                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 企业用户--查看 审核的岗位信息 及审核状态
@bp.route("/eGetAuditJobs", methods=["POST"])
def eGetAuditJobs():
    global t
    t=""
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "hrname": [],
        "confirmtime": [],
        "audittime": [],
        "auditstate": [],
        "operationtype": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                job_lis = []
                hrname_lis = []
                confirmtime_lis = []
                audittime_lis = []
                auditstate_lis = []
                operationType_lis=[]
                if hr:
                    jobs = EJobConfirmingModel.query.all()
                    if jobs:
                        for job in jobs:
                            auditHR = HRModel.query.filter_by(hrId=job.hrId).first()
                            # 获取的岗位中的 hrId和当前hr的hrId相同 则添加
                            if auditHR.e_hr_id == hr.e_hr_id:
                                if job.operationType=="update":
                                    t="编辑岗位"

                                elif job.operationType=="add":
                                    t="添加岗位"
                                msg["datas"].append(
                                    {
                                        "auditId": job.auditId,
                                        "jobId": job.jobId,
                                        "jobName": job.jobName,
                                        "jobIndustry":job.jobIndustry.split("-")[1],
                                        "hrId": job.hrId,
                                        "hrName": job.hrName,
                                        "isAudit": job.isAudit,
                                        "auditTime": job.auditTime if job.auditTime else 'null',
                                        "confirmTime": job.confirmTime,
                                        "auditState": job.auditState,
                                        "operationType": t,
                                        # "eId": hr.e_hr_id,
                                        # "businessName": hr.e_hr.businessName
                                    }
                                )
                                job_lis.append(
                                    {
                                        "text": job.jobIndustry.split("-")[1],
                                        "value": job.jobIndustry.split("-")[1],
                                    }
                                )
                                hrname_lis.append(
                                    {
                                        "text": job.hrName,
                                        "value": job.hrName,
                                    }
                                )

                                confirmtime_lis.append(
                                    {
                                        "text": job.confirmTime.split(" ")[0],
                                        "value": job.confirmTime.split(" ")[0],
                                    }
                                )

                                if job.auditTime is not None:

                                    audittime_lis.append(
                                        {
                                            "text": job.auditTime.split(" ")[0],
                                            "value": job.auditTime.split(" ")[0],
                                        }
                                    )
                                else:
                                    audittime_lis.append(
                                        {
                                            "text": "null",
                                            "value": "null",
                                        }
                                    )
                                auditstate_lis.append(
                                    {
                                        "text": job.auditState,
                                        "value": job.auditState,
                                    }
                                )
                                operationType_lis.append(
                                    {
                                        "text": t,
                                        "value": t,
                                    }
                                )
                        for job in job_lis:
                            if job not in msg["industrys"]:
                                msg["industrys"].append(job)
                        for hr in hrname_lis:
                            if hr not in msg["hrname"]:
                                msg['hrname'].append(hr)
                        for confirm in confirmtime_lis:
                            if confirm not in msg["confirmtime"]:
                                msg['confirmtime'].append(confirm)
                        for audit in audittime_lis:
                            if audit not in msg["audittime"]:
                                msg['audittime'].append(audit)

                        for state in auditstate_lis:
                            if state not in msg["auditstate"]:
                                msg['auditstate'].append(state)
                        for operation in operationType_lis:
                            if operation not in msg["operationtype"]:
                                msg['operationtype'].append(operation)

                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 企业用户--查看 具体的 审核的企业信息 及审核状态
@bp.route("/ePreAuditEnterprise", methods=["POST"])
def ePreAuditEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    auditId = request.json["auditId"]
                    e = EInfoConfirmingModel.query.filter_by(auditId=auditId).first()
                    if e:
                        msg["datas"].append(
                            {
                                "auditId": e.auditId,
                                "eId": e.eId,
                                "eContent": e.content,
                                "eSize": e.size,
                                "eNature": e.nature,
                                "eIndustry": e.industry,
                                "eAdvantage": "，".join(e.advantage.split("-")),

                                "contactName": e.contactName,
                                "contactPhone": e.contactPhone,
                                "contactEmail": e.contactEmail,
                                "contactAddress": e.contactAddress,

                                "businessName": e.businessName,
                                "businessSetTime": e.businessSetTime,
                                "businessRegCapital": e.businessRegCapital,
                                "businessLegalRepresentative": e.businessLegalRepresentative,

                                "hrId": e.hrId,
                                "hrName": e.hrName,
                                "hrEmail": e.hrEmail,
                                "hrPhone": e.hrPhone,

                                "isAudit": e.isAudit,
                                "auditTime": e.auditTime,
                                "confirmTime": e.confirmTime,
                                "auditState": e.auditState,
                                "operationType": e.operationType,
                                "auditResultContent": e.auditResultContent,
                            }
                        )
                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 企业用户--查看 审核的企业信息 及审核状态
@bp.route("/eGetAuditEnterprise", methods=["POST"])
def eGetAuditEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "hrname": [],
        "confirmtime": [],
        "audittime": [],
        "auditstate": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]
    hrname_lis=[]
    confirmtime_lis=[]
    audittime_lis=[]
    auditstate_lis=[]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    es = EInfoConfirmingModel.query.filter_by(eId=hr.e_hr_id).all()
                    if es:
                        for e in es:
                            msg["datas"].append(
                                {
                                    "auditId": e.auditId,
                                    "eId": e.eId,
                                    "businessName": e.businessName,

                                    "hrId": e.hrId,
                                    "hrName": e.hrName,
                                    # "hrEmail": e.hrEmail,
                                    "hrPhone": e.hrPhone,

                                    "isAudit": e.isAudit,
                                    "auditTime": e.auditTime if e.auditTime else 'null',
                                    "confirmTime": e.confirmTime,
                                    "auditState": e.auditState,
                                    # "operationType": e.operationType,
                                    # "auditResultContent": e.auditResultContent,
                                }
                            )

                            hrname_lis.append(
                                {
                                    "text": e.hrName,
                                    "value": e.hrName,
                                }
                            )

                            confirmtime_lis.append(
                                {
                                    "text": e.confirmTime.split(" ")[0],
                                    "value": e.confirmTime.split(" ")[0],
                                }
                            )

                            if e.auditTime is not None:

                                audittime_lis.append(
                                    {
                                        "text": e.auditTime.split(" ")[0],
                                        "value": e.auditTime.split(" ")[0],
                                    }
                                )
                            else:

                                audittime_lis.append(
                                    {
                                        "text": "null",
                                        "value": "null",
                                    }
                                )
                            auditstate_lis.append(
                                {
                                    "text": e.auditState,
                                    "value": e.auditState,
                                }
                            )
                        for hr in hrname_lis:
                            if hr not in msg["hrname"]:
                                msg['hrname'].append(hr)
                        for confirm in confirmtime_lis:
                            if confirm not in msg["confirmtime"]:
                                msg['confirmtime'].append(confirm)
                        for audit in audittime_lis:
                            if audit not in msg["audittime"]:
                                msg['audittime'].append(audit)

                        for state in auditstate_lis:
                            if state not in msg["auditstate"]:
                                msg['auditstate'].append(state)

                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取公司头像
@bp.route("/getEAvatar", methods=["POST"])
def getEAvatar():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    eId = request.json["eId"]
    if eId:
        e = EnterpriseModel.query.filter_by(eId=eId).first()
        if e:
            # 拼接图片地址
            image_url = BASE_DIR / "static/img" / e.avatarName
            with open(image_url, "rb") as f:
                image_data = f.read()
            image_res = make_response(image_data)
            image_res.headers['Content-Type'] = 'image.jpeg/png'
            return image_res
        else:
            msg["message"] = "该公司不存在"
            msg["code"] = "500"
            msg["type"] = "error"
            return msg
    else:
        msg["message"] = "eId获取失败"
        msg["code"] = "500"
        msg["type"] = "error"
        return msg


# 更新hr本人的头像
@bp.route("/upHRAvatar", methods=["POST"])
def upHRAvatar():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    loginId = request.form["loginId"]
    # 获取用户的身份
    identity = request.form["identity"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    # 请求中获取到上传的图片
                    avatar_data = request.files.get('file')
                    # 获取头像具体格式，jpg、jpeg、、、、
                    avatar_type = avatar_data.content_type.split("/")
                    # 随机头像在本地的名称
                    avatar_name = hook.getRandom() + "." + avatar_type[1]
                    # 保存头像的路径
                    path = BASE_DIR / "static/img/"
                    # 头像的绝对路径
                    avatar_path = os.path.join(path, avatar_name)
                    avatar_data.save(avatar_path)  # 保存

                    hr.hrAvatarName = avatar_name

                    msg["message"] = "上传成功"
                    msg["type"] = "success"
                    msg["code"] = "200"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 更新hr本人的信息
@bp.route("/upEHRInfo", methods=["POST"])
def upEHRInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                ejobs = EJobConfirmingModel.query.filter_by(hrId=loginId).all()
                if hr:
                    hrEmail=request.json["hrEmail"]
                    hrPhone=request.json["hrPhone"]
                    # 修改邮箱，电话
                    # 判断是不是本人的邮箱和电话，为真，则允许修改
                    e_hr=HRModel.query.filter_by(hrEmail=hrEmail).first()
                    p_hr=HRModel.query.filter_by(hrPhone=hrPhone).first()
                    # 存在相同的邮箱或电话
                    if e_hr and p_hr:
                        # 本人修改了相同的邮箱电话,允许修改
                        if e_hr.hrId==hr.hrId and p_hr.hrId==hr.hrId:
                            msg["message"] = "修改成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                            hr.hrName = request.json["hrName"]
                            hr.hrGender = request.json["hrGender"]
                            hr.hrEmail = hrEmail
                            hr.hrPhone = hrPhone
                            hr.hrSignature = request.json["hrSignature"]
                            db.session.commit()
                        elif e_hr.hrId==hr.hrId and p_hr.hrId != hr.hrId:
                            # 存在不是本人的电话,不可修改
                            msg["message"] = "此联系方式不可使用，请更换"
                            msg["code"] = "301"
                            msg["type"] = "error"
                        elif e_hr.hrId!=hr.hrId and p_hr.hrId == hr.hrId:
                            # 存在不是本人的邮箱，不可修改
                            msg["message"] = "此邮箱不可使用，请更换"
                            msg["code"] = "301"
                            msg["type"] = "error"
                        elif e_hr.hrId != hr.hrId and p_hr.hrId!=hr.hrId:
                            # 存在不是本人的邮箱和电话
                            msg["message"] = "此邮箱、联系方式不可使用，请更换"
                            msg["code"] = "301"
                            msg["type"] = "error"

                    elif e_hr and p_hr is None:
                        # 存在相同的邮箱,不存在联系方式
                        if e_hr.hrId != hr.hrId:
                            # 存在不是本人的邮箱，不可修改
                            msg["message"] = "此邮箱不可使用，请更换"
                            msg["code"] = "301"
                            msg["type"] = "error"
                        elif e_hr.hrId == hr.hrId:
                            # 存在是本人的邮箱,允许修改
                            msg["message"] = "修改成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                            hr.hrName = request.json["hrName"]
                            hr.hrGender = request.json["hrGender"]
                            hr.hrEmail = hrEmail
                            hr.hrPhone = hrPhone
                            hr.hrSignature = request.json["hrSignature"]
                            db.session.commit()
                    elif e_hr is None and p_hr:
                        # 不存在邮箱,存在联系方式
                        if p_hr.hrId==hr.hrId:
                            # 相同本人的联系方式,允许修改
                            msg["message"] = "修改成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                            hr.hrName = request.json["hrName"]
                            hr.hrGender = request.json["hrGender"]
                            hr.hrEmail = hrEmail
                            hr.hrPhone = hrPhone
                            hr.hrSignature = request.json["hrSignature"]
                            db.session.commit()
                        elif p_hr.hrId!= hr.hrId:
                            # 不同本人的联系方式,说明有人使用了该联系方式,不允许修改
                            msg["message"] = "此联系方式不可使用，请更换"
                            msg["code"] = "301"
                            msg["type"] = "error"
                    elif e_hr is None and p_hr is None:
                        # 邮箱和联系方式都不存在,允许修改
                        msg["message"] = "修改成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                        hr.hrName = request.json["hrName"]
                        hr.hrGender = request.json["hrGender"]
                        hr.hrEmail = hrEmail
                        hr.hrPhone = hrPhone
                        hr.hrSignature = request.json["hrSignature"]
                        db.session.commit()
                    else:
                        # 不存在该邮箱,允许修改
                        msg["message"] = "修改成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                        hr.hrName = request.json["hrName"]
                        hr.hrGender = request.json["hrGender"]
                        hr.hrEmail = hrEmail
                        hr.hrPhone = request.json["hrPhone"]
                        hr.hrSignature = request.json["hrSignature"]
                        db.session.commit()

                    # # 不是本人的邮箱和电话，为真，则不允许修改
                    # if hr.hrEmail != request.json["hrEmail"]:
                    #     msg["message"] = "您邮箱已被修改，请重新登录"
                    #     msg["code"] = "201"
                    #     msg["type"] = "success"
                    # else:
                    #     msg["message"] = "修改成功"
                    #     msg["code"] = "200"
                    #     msg["type"] = "success"
                    #     hr.hrName = request.json["hrName"]
                    #     hr.hrGender = request.json["hrGender"]
                    #     hr.hrEmail = request.json["hrEmail"]
                    #     hr.hrPhone = request.json["hrPhone"]
                    #     hr.hrSignature = request.json["hrSignature"]
                    #     db.session.commit()
                        if ejobs:
                            for ejob in ejobs:
                                if (ejob.hrId == hr.hrId):
                                    ejob.hrName = request.json["hrName"]
                                    db.session.commit()

                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 更新hr本人密码
@bp.route("/upEHRPwd", methods=["POST"])
def upEHRPwd():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    if check_password_hash(hr.hrPassword, request.json["oldPwd"]):
                        hr.hrPassword = generate_password_hash(request.json["newPwd"])
                        db.session.commit()
                        msg["message"] = "密码修改成功，请重新登录"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "密码错误，请重新填写"
                        msg["code"] = "501"
                        msg["type"] = "error"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取hr本人的信息
@bp.route("/getEHRInfo", methods=["POST"])
def getEHRInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()

                if hr:
                    msg["hrId"] = hr.hrId
                    msg["hrName"] = hr.hrName
                    msg["hrGender"] = hr.hrGender
                    msg["hrEmail"] = hr.hrEmail
                    msg["hrPhone"] = hr.hrPhone
                    msg["hrEnterprise"] = hr.e_hr.businessName
                    msg["hrLevel"] = hr.hrLevel
                    msg["hrSignature"] = hr.hrSignature
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"

                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 预览用户简历
@bp.route("/getEUserResume", methods=["POST"])
def getEUserResume():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    # 获取hr所在企业所有的hr
                    uId = request.json["uId"]
                    user = UserModel.query.filter_by(uId=uId).first()
                    userResume = UserResumeModel.query.filter_by(uId=uId).first()

                    msg["uId"] = user.uId
                    msg["gander"] = user.gander if user.gander else ""
                    msg["name"] = user.name
                    msg["birth"] = user.birth if user.birth else "-"
                    msg["newAddress"] = user.newAddress if user.newAddress else ""
                    msg["politicalOutlook"] = user.politicalOutlook if user.politicalOutlook else ""
                    msg["phone"] = user.phone if user.phone else ""
                    msg["email"] = user.email

                    msg["jobTitle"] = userResume.jobTitle if userResume and userResume.jobTitle else ""
                    msg["jobSalary"] = userResume.jobSalary if userResume and userResume.jobSalary else ""
                    msg["jobIndustry"] = userResume.jobIndustry.split("-")[1] if userResume and userResume.jobIndustry else ""

                    msg["schoolTime"] = userResume.schoolTime if userResume and userResume.schoolTime else ""
                    msg["eduSchoolName"] = userResume.eduSchoolName if userResume and userResume.eduSchoolName else ""
                    msg["eduMajor"] = userResume.eduMajor if userResume and userResume.eduMajor else ""
                    msg["eduEducation"] = userResume.eduEducation if userResume and userResume.eduEducation else ""
                    msg["eduMajorDetail"] = userResume.eduMajorDetail if userResume and userResume.eduMajorDetail else ""

                    msg["jobExTime"] = userResume.jobExTime if userResume and userResume.jobExTime else ""
                    msg["jobExEnterprise"] = userResume.jobExEnterprise if userResume and userResume.jobExEnterprise else ""
                    msg["jobExLevel"] = userResume.jobExLevel if userResume and userResume.jobExLevel else ""
                    msg["jobExDetail"] = userResume.jobExDetail if userResume and userResume.jobExDetail else ""

                    msg["projectTime"] = userResume.projectTime if userResume and userResume.projectTime else ""
                    msg["projectEnterprise"] = userResume.projectEnterprise if userResume and userResume.projectEnterprise else ""
                    msg["projectName"] = userResume.projectName if userResume and userResume.projectName else ""
                    msg["projectDetail"] = userResume.projectDetail if userResume and userResume.projectDetail else ""
                    msg["userHonors"] = userResume.userHonors.split("-") if userResume and userResume.userHonors else ""
                    msg["userSelfEvaluation"] = userResume.userSelfEvaluation if userResume and userResume.userSelfEvaluation else ""
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 更新简历通过与否
@bp.route("/upEUserResumeState", methods=["POST"])
def upEUserResumeState():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    # 获取hr所在企业所有的hr
                    jobId = request.json["jobId"]
                    uId = request.json["uId"]
                    userAndJobModel = UserAndJobModel.query.filter_by(jobId=jobId, uId=uId).first()
                    if userAndJobModel:
                        userAndJobModel.applyState = request.json["uApplyState"]
                        userAndJobModel.stateContent = request.json["uStateContent"]

                        db.session.commit()
                        msg["message"] = "操作成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "操作失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取用户申请的岗位，
@bp.route("/getEUserApplyJobs", methods=["POST"])
def getEUserApplyJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "applyresumename": [],
        "applyresumenature": [],
        "applyresumejobname": [],
        "applyresumestate": [],
    }
    global data_id
    data_id = 0
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                uName_lis=[]
                uNatue_lis=[]
                jobName_lis=[]
                state_lis=[]
                if hr:
                    # 获取hr所在企业所有hr所发布的岗位，即该企业所发布的全部岗位
                    for j in hr.hr_jobs:

                        # 获取该公司被用户申请的所有岗位
                        userAndJobModel = UserAndJobModel.query.filter_by(jobId=j.jobId).all()
                        if userAndJobModel:
                            for userJob in userAndJobModel:
                                data_id = data_id + 1
                                # 通过岗位和求职者关系表查找到对应的uid，再查找对应的求职者，之后再查找求职者简历
                                user = UserModel.query.filter_by(uId=userJob.uId).first()
                                userResume = UserResumeModel.query.filter_by(uId=userJob.uId).first()

                                msg["datas"].append(
                                    {
                                        "id":str(data_id),
                                        "uId": userJob.uId,
                                        "uApplyState": userJob.applyState,
                                        # "uStateContent": userJob.stateContent,
                                        "uName": user.name,
                                        "uNature": userResume.jobNature if userResume and userResume.jobNature else "",
                                        "uSalary": userResume.jobSalary if userResume and userResume.jobSalary else "",
                                        "jobName": j.jobName ,
                                        "jobId": j.jobId,
                                    }
                                )
                                uName_lis.append(
                                    {
                                        "text": user.name,
                                        "value": user.name,
                                    }
                                )
                                uNatue_lis.append(
                                    {
                                        "text": userResume.jobNature if userResume and userResume.jobNature else "",
                                        "value":userResume.jobNature if userResume and userResume.jobNature else "",
                                    }
                                )
                                jobName_lis.append(
                                    {
                                        "text": j.jobName,
                                        "value": j.jobName,
                                    }
                                )
                                state_lis.append(
                                    {
                                        "text": userJob.applyState,
                                        "value": userJob.applyState,
                                    }
                                )
                    for uname in uName_lis:
                        if uname not in msg["applyresumename"]:
                            msg["applyresumename"].append(uname)
                    for nature in uNatue_lis:
                        if nature not in msg["applyresumenature"]:
                            msg["applyresumenature"].append(nature)
                    for jobname in jobName_lis:
                        if jobname not in msg["applyresumejobname"]:
                            msg["applyresumejobname"].append(jobname)
                    for state in state_lis:
                        if state not in msg["applyresumestate"]:
                            msg["applyresumestate"].append(state)
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"


                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 停招岗位
@bp.route("/stopPublishingJob", methods=["POST"])
def stopPublishingJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    jobId = request.json["jobId"]
                    jobModel = JobModel.query.filter_by(jobId=jobId).first()
                    if jobModel:
                        jobModel.isPublishing = "false"
                        jobModel.jobState = "已停止招聘"
                        db.session.commit()
                        msg["message"] = "操作成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "岗位已被删除，操作失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 将hr修改的岗位信息提交，让admin审核
@bp.route("/confirmPublishingJob", methods=["POST"])
def confirmPublishingJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    # print(request.json["jobIndustry"])
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:

                    eJobConfirmingModel = EJobConfirmingModel(
                        auditId=str(int(time.time() * 1000)),
                        jobId=request.json["jobId"],
                        jobName=request.json["jobName"],
                        jobNature=request.json["jobNature"],
                        jobEdu=request.json["jobEdu"],
                        jobAge=request.json["jobAge"],
                        jobEx=request.json["jobEx"],
                        jobAddress=request.json["jobAddress"],
                        jobSalary=request.json["jobSalary"],
                        # jobIndustry=request.json["jobIndustry"][1],
                        jobIndustry=request.json["jobIndustry"][0]+"-"+request.json["jobIndustry"][1],
                        jobDetail=request.json["jobDetail"],
                        operationType=request.json["operationType"],
                        hrId=hr.hrId,
                        hrName=hr.hrName,
                        hrEmail=hr.hrEmail,
                        hrPhone=hr.hrPhone,
                        isAudit="false",
                        confirmTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
                        auditState="待审核",
                    )
                    db.session.add(eJobConfirmingModel)
                    db.session.commit()

                    msg["message"] = "提交成功，待审核"
                    msg["code"] = "200"
                    msg["type"] = "success"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取所有停招岗位
@bp.route("/getAllPublishedJob", methods=["POST"])
def getAllPublishedJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "hrs": [],

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                eId = HRModel.query.filter_by(hrId=loginId).first().e_hr_id
                hrs = HRModel.query.filter_by(e_hr_id=eId).all()
                job_lis = []
                hr_lis = []
                if hrs:
                    for i in hrs:
                        if i.hrId == loginId:
                            isCurrentHR = "true"
                        else:
                            isCurrentHR = "false"
                        for j in i.hr_jobs:
                            if j.isPublishing == "false":
                                msg["datas"].append(
                                    {
                                        "jobId": j.jobId,
                                        "hrName": i.hrName,
                                        "hrPhone": i.hrPhone,
                                        "jobName": j.jobName,
                                        "jobNature": j.jobNature,
                                        "jobAddress": j.jobAddress,
                                        "jobEdu": j.jobEdu,
                                        "jobAge": j.jobAge,
                                        "jobEx": j.jobEx,
                                        "jobSalary": j.jobSalary,
                                        "jobDetail": j.jobDetail,
                                        # "jobIndustry": j.jobIndustry,
                                        "jobIndustry":
                                            {"0": j.jobIndustry.split("-")[0], "1": j.jobIndustry.split("-")[1]},
                                        "jobState": j.jobState,
                                        "isCurrentHR": isCurrentHR,
                                    }
                                )
                                job_lis.append(
                                    {
                                        "text": j.jobIndustry.split("-")[1],
                                        "value": j.jobIndustry.split("-")[1],
                                    }
                                )
                                hr_lis.append(
                                    {
                                        "text": i.hrName,
                                        "value": i.hrName,
                                    }
                                )
                    for job in job_lis:
                        if job not in msg["industrys"]:
                            msg["industrys"].append(job)
                    for hr in hr_lis:
                        if hr not in msg["hrs"]:
                            msg["hrs"].append(hr)
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"

                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取所有在招岗位
@bp.route("/getAllPublishingJob", methods=["POST"])
def getAllPublishingJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "hrs": [],

    }

    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                # 获取当前hr的eId
                eId = HRModel.query.filter_by(hrId=loginId).first().e_hr_id
                hrs = HRModel.query.filter_by(e_hr_id=eId).all()
                job_lis = []
                hr_lis = []
                if hrs:
                    for i in hrs:
                        # print(i)
                        if i.hrId == loginId:
                            isCurrentHR = "true"
                        else:
                            isCurrentHR = "false"
                        for j in i.hr_jobs:
                            # print(j.jobId)
                            if j.isPublishing == "true":
                                msg["datas"].append(
                                    {

                                        "jobId": j.jobId,
                                        "hrName": i.hrName,
                                        "hrPhone": i.hrPhone,
                                        "jobName": j.jobName,
                                        "jobNature": j.jobNature,
                                        "jobAddress": j.jobAddress,
                                        "jobEdu": j.jobEdu,
                                        "jobAge": j.jobAge,
                                        "jobEx": j.jobEx,
                                        "jobSalary": j.jobSalary,
                                        "jobDetail": j.jobDetail,
                                        "jobIndustry":
                                            {"0": j.jobIndustry.split("-")[0], "1": j.jobIndustry.split("-")[1]},
                                        # "jobIndustry": j.jobIndustry.split("-"),
                                        "jobState": j.jobState,
                                        "isCurrentHR": isCurrentHR,
                                        "isPublishing": j.isPublishing,
                                    }
                                )
                                job_lis.append(
                                    {
                                        "text": j.jobIndustry.split("-")[1],
                                        "value": j.jobIndustry.split("-")[1],
                                    }
                                )
                                hr_lis.append(
                                    {
                                        "text": i.hrName,
                                        "value": i.hrName,
                                    }
                                )
                    for job in job_lis:
                        if job not in msg["industrys"]:
                            msg["industrys"].append(job)
                    for hr in hr_lis:
                        if hr not in msg["hrs"]:
                            msg["hrs"].append(hr)
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"

                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 将hr修改的企业信息修改，让admin审核
@bp.route("/confirmEInfo", methods=["POST"])
def confirmEInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    eInfoConfirming = EInfoConfirmingModel(
                        auditId=str(int(time.time() * 1000)),
                        eId=hr.e_hr_id,
                        content=request.json["EContent"],
                        size=request.json["ESizeMin"] + "-" + request.json["ESizeMax"],
                        nature=request.json["ENature"],
                        industry=request.json["EIndustry"],
                        advantage="-".join(request.json["EAdvantage"]),
                        contactName=request.json["contactName"],
                        contactPhone=request.json["contactPhone"],
                        contactEmail=request.json["contactEmail"],
                        contactAddress=request.json["contactAddress"],
                        businessName=request.json["businessName"],
                        businessSetTime=request.json["businessSetTime"],
                        businessRegCapital=request.json["businessRegCapital"],
                        businessLegalRepresentative=request.json["businessLegalRepresentative"],
                        hrId=hr.hrId,
                        hrName=hr.hrName,
                        hrEmail=hr.hrEmail,
                        hrPhone=hr.hrPhone,

                        isAudit="false",
                        confirmTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
                        operationType="修改企业信息",
                        auditState="待审核",
                    )
                    db.session.add(eInfoConfirming)
                    db.session.commit()
                    msg["message"] = "提交成功"
                    msg["code"] = "200"
                    msg["type"] = "success"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取hr的头像
@bp.route("/getHRAvatar", methods=["POST"])
def getHRAvatar():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    hrId = request.json["loginId"]

    hr = HRModel.query.filter_by(hrId=hrId).first()
    if hr:
        # 拼接图片地址
        image_url = BASE_DIR / "static/img" / hr.hrAvatarName
        # image_url = BASE_DIR / "static/avatar_1.jpeg"
        # print(user.avatarName)
        with open(image_url, "rb") as f:
            image_data = f.read()
        image_res = make_response(image_data)
        image_res.headers['Content-Type'] = 'image.jpeg/png'
        return image_res
    else:
        msg["message"] = "hr不存在"
        msg["code"] = "500"
        msg["type"] = "error"
        return msg


# 获取企业基本信息
@bp.route("/getEAllInfo", methods=["POST"])
def getEAllInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "enterprise":
                hr = HRModel.query.filter_by(hrId=loginId).first()
                if hr:
                    e = EnterpriseModel.query.filter_by(eId=hr.e_hr_id).first()
                    msg["ENature"] = e.nature
                    msg["ESizeMin"] = e.size.split("-")[0]
                    msg["ESizeMax"] = e.size.split("-")[1]
                    msg["EIndustry"] = e.industry
                    msg["EContent"] = e.content
                    msg["EAdvantage"] = e.advantage.split("-") if e.advantage else ""
                    msg["contactName"] = e.contactName
                    msg["contactPhone"] = e.contactPhone
                    msg["contactEmail"] = e.contactEmail
                    msg["contactAddress"] = e.contactAddress
                    msg["businessName"] = e.businessName
                    msg["businessSetTime"] = e.businessSetTime
                    msg["businessRegCapital"] = e.businessRegCapital
                    msg["businessLegalRepresentative"] = e.businessLegalRepresentative
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"
                else:
                    msg["message"] = "该hrId非法"
                    msg["code"] = "500"
                    msg["type"] = "error"
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg

# def jobIndustryMenuTrees(parentId="0"):
#     '''
#     通过递归实现根据父ID查找子菜单,如果传入用户id则只查询该用户的权限否则查询所有权限,一级菜单父id默认是0
#     1.根据父ID获取该菜单下的子菜单或权限
#     2.遍历子菜单或权限，继续向下获取，直到最小级菜单或权限
#     3.如果没有遍历到，返回空的数组，有返回权限列表
#     :param user_id:
#     :param parentId:
#     :return:dict
#     '''
#
#     menu_data = HomeMenuModel.query.filter(HomeMenuModel.father == parentId).order_by('id').all()
#     menu_dict = jobIndustryMenu_to_dict(menu_data)
#     if len(menu_dict) > 0:
#         data = []
#         for menu in menu_dict:
#             menu['children'] = jobIndustryMenuTrees(menu['id'])
#             if len(menu["id"])<4 :
#                 if menu["id"][0:2]=="10" or len(menu["id"])<3:
#                     data.append(menu)
#         return data
#     return []
#
# def jobIndustryMenu_to_dict(result):
#     '''
#         格式化菜单字段显示顺序
#         :param result:
#         :return:
#         '''
#     data = []
#     for menu in result:
#         children = {
#             "id": menu.children,
#             "value": menu.name,
#             "label": menu.name,
#             # "parent_id": menu.parent_id,
#         }
#         data.append(children)
#     return data
